<!--
 * @Author: 雷宇航
 * @Date: 2021-07-28 13:57:40
 * @LastEditTime: 2021-08-03 10:50:05
 * @LastEditors: 雷宇航
 * @Description: 
 * @FilePath: \icm-vue\src\views\hifar\postsale\modules\PostSaleQuestionItemForm.vue
-->
<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model
        :model="form"
        slot="detail"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        ref="form"
        :rules="validatorRules"
      >
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="问题类型">
              <j-dict-select-tag
                type="list"
                v-model="form.questionType"
                :trigger-change="true"
                dictCode="icm_post_sale_question_type"
                placeholder="请选择问题类型"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问题描述">
              <a-textarea v-model="form.questionDesc" disabled rows="10"></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工时" prop="workHours" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-input v-model="form.workHours"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="完成人" prop="charger" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <j-select-user-by-dep :multi="false" v-model="form.charger" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="完成时间" prop="completedDate" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <j-date placeholder="请选择完成时间" v-model="form.completedDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="问题状态" prop="questionStatus" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <j-dict-select-tag
                type="list"
                v-model="form.questionStatus"
                dictCode="icm_post_sale_question_status"
                placeholder="请选择问题状态"
              />
            </a-form-model-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm" type="primary">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import { newJEditableTableMixin } from '@/mixins/newJEditableTableMixin'

export default {
  name: 'PostSaleQuestionItemForm',
  mixins: [newJEditableTableMixin],
  components: {},
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      form: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
      confirmLoading: false,
      validatorRules: {
        workHours: [{ required: true, message: '请输入工时!' }],
        charger: [{ required: true, message: '请输入完成人!' }],
        completedDate: [{ required: true, message: '请选择完成时间!' }],
        questionStatus: [{ required: true, message: '请选择问题状态!' }],
      },
      url: {
        edit: '/postsale/postSaleQuestionItem/edit',
        queryById: '/postsale/postSaleQuestionItem/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    },
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      // this.form.resetFields()
      this.$refs.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.form = this.model
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, this.form)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    popupCallback(row) {
      this.form = Object.assign({}, this.form, row)
    },
  },
}
</script>